﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter20
{
    using System.IO;
    using System.Runtime.CompilerServices;

    partial class Program
    {
        private delegate void Feedback(int value);

        internal static void Begin(Action action, bool active = false)
        {
            if (active)
            {
                Console.WriteLine("开始演示：");
                action();
                Console.WriteLine("演示完毕：");
                Console.ReadLine();
            }
        }

        private class Accout
        {
            private decimal amout;

            public Accout(decimal amout)
            {
                this.amout = amout;
            }

            public decimal Amout
            {
                get { return amout; }
                set { amout = value; }
            }

            public static Accout operator +(Accout left, Accout right)
            {
                var accout = new Accout(0);
                accout.Amout = left.amout + right.amout;
                return accout;
            }

            public static Accout operator -(Accout left, Accout right)
            {
                return left - right;
            }

            public static implicit operator decimal(Accout accout)
            {
                return accout.Amout;
            }

            public static implicit operator Accout(decimal amout)
            {
                return new Accout(amout);
            }

            public static void Transfer(Accout from, Accout to, decimal amout)
            {
                from -= amout;
                to += amout;
            }
        }

        internal static void C01()
        {
            var f = "Jeff".Substring(1, 1).ToUpper().EndsWith("E");
        }

        private static void SomeMethod()
        {
            try
            {

            }
            catch (InvalidOperationException)
            {

                throw;
            }
            catch (IOException)
            {
                throw;
            }
            catch
            {
                throw;
            }
            finally
            {

            }
        }

        private void ReadData(string pathName)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(pathName, FileMode.Open);
            }
            catch (IOException)
            {

            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

        private static void SomeMethod2()
        {
            try
            {

            }
            catch (Exception e)
            {

                throw e;
            }
            try
            {

            }
            catch (Exception)
            {

                throw;
            }
        }

        private static void SomeMethod3()
        {
            var trySucceeds = false;
            try
            {
                trySucceeds = true;
            }
            finally
            {
                if (!trySucceeds)
                {

                }
            }
        }

        private class SomeType
        {
            [MethodImpl(MethodImplOptions.NoInlining]    
            public void SomeMethod()
            {

            }
        }
    }
}
